package com.wuliu.driver.controller;

import com.wuliu.driver.util.Code;
import com.wuliu.driver.util.MapUtil;
import com.wuliu.driver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/map")
public class MapController {
    @Autowired
    private MapUtil mapUtil;
    //根据地址获取坐标
    @GetMapping("/get/{address}")
    public Result get(@PathVariable String address) throws IOException {
        Map geocode = mapUtil.geocode(address);
        if (!geocode.isEmpty()){
            return new Result(Code.GET_OK,geocode,"查询成功");
        }
        return new Result(Code.GET_ERR,"查询成功");
    }

    //根据坐标获取地址
    @GetMapping("/reverse/{latitude}/{longitude}")
    public Result reverse(@PathVariable String latitude,@PathVariable String longitude) throws IOException {
        String s = mapUtil.parseResult(Double.parseDouble(latitude), Double.parseDouble(longitude));
        if (s==null && s.equals("")){
            return new Result(Code.GET_ERR,"查询失败");
        }
        return new Result(Code.GET_OK,s,"查询成功");
    }

    //前端发送过来一个字符串其中包含经纬度，根据这个经纬度查询这个地址
    @PostMapping("/getad")
    public Result reverse(@RequestBody Map address) throws IOException {
        double latitude = Double.parseDouble(address.get("h_latitude").toString());
        double longitude = Double.parseDouble(address.get("h_longitude").toString());
        //String[] split = address.split(",");
//        double  latitude = Double.parseDouble(split[1]);
//        double longitude = Double.parseDouble(split[0]);
        String s = mapUtil.parseResult(latitude,longitude);
        if (s==null && s.equals("")){
            return new Result(Code.GET_ERR,"查询失败");
        }
        return new Result(Code.GET_OK,s,"查询成功");
    }

    @PostMapping("/getwd")
    public Result getwd(@RequestBody Map map) throws IOException {
        System.out.println(map);
        double h_latitude = Double.parseDouble(map.get("h_latitude").toString());
        double h_longitude = Double.parseDouble(map.get("h_longitude").toString());
        double latitude = Double.parseDouble(map.get("latitude").toString());
        double longitude = Double.parseDouble(map.get("longitude").toString());
        double distance = mapUtil.getDistance(h_latitude, h_longitude, latitude, longitude);
        System.out.println(distance);
        return new Result(Code.GET_OK,distance,"查询成功");
    }

}
